import NetManager from "../../../Framework/Scripts/Net/NetManager";
import { SDK, ShareDefine } from "../Config/ShareDefine";

const {ccclass, property} = cc._decorator;

/**
 * 登录的所有数据
 */

const LoginMsg = {
    LoginYK:0xFF00, //游客

    LoginAccount:0xFF03,//账号
}

@ccclass
export default class LoginMgr extends cc.Component {
    public static Instance: LoginMgr = null;
    dataInfo: any;
    logInAccountList: [];
    localAccountDict: {};
    onLoad() {
        if (LoginMgr.Instance !== null) {
            this.destroy();
            return;
        }
        LoginMgr.Instance = this;
    }

    Init(){
        console.warn("===========LoginMgr init=============");
    }

    OneKeyRegAccount(){
        console.warn("===========LoginMgr OneKeyRegAccount=============");
        let sendPack = NetManager.Instance.GetHttpSendPack(LoginMsg.LoginYK);
        NetManager.Instance.SendHttpPack(sendPack, this.OnHttpPack_AccountLogin);
    }

    OnHttpPack_AccountLogin(serverPack){
        console.log("=============OnHttpPack_AccountLogin===============",serverPack);
        this.dataInfo = serverPack;

        //每次登陆都覆盖缓存的sdkToken(客户端只记录一个玩家登陆token)
        let sdkToken = this.dataInfo["SDKToken"];
        let accountType = this.dataInfo["AccountType"];
        let accountID = this.dataInfo["AccountID"];

        let accessTokenInfo = {};
        //如果是微信APP,sdk授权登录
        if (sdkToken && accountType == SDK.SDKType_WeChatApp) {
            accessTokenInfo = {
                "SDKToken": sdkToken,
                "AccountType": accountType,
                "AccountID": accountID,
            };
        }
        if (sdkToken && accountType == SDK.SDKType_LineApp) {
            accessTokenInfo = {
                "SDKToken": sdkToken,
                "AccountType": accountType,
                "AccountID": accountID,
                "headImgUrl": this.dataInfo["HeadImageUrl"],
                "nickName": this.dataInfo["NickName"],
                "userId": this.dataInfo["Unionid"],
                "accessToken": this.dataInfo["SDKToken"],
            };
        }
        if (sdkToken && accountType == SDK.SDKType_FacebookApp) {
            accessTokenInfo = {
                "SDKToken": sdkToken,
                "AccountType": accountType,
                "AccountID": accountID,
                "headImgUrl": this.dataInfo["HeadImageUrl"],
                "nickName": this.dataInfo["NickName"],
                "userId": this.dataInfo["Unionid"],
                "accessToken": this.dataInfo["SDKToken"],
            };
        }
        console.error("======hall OnHttpPack_AccountLogin sdkToken:%s, accessTokenInfo:%s", sdkToken, JSON.stringify(accessTokenInfo));
        console.error("======hall OnHttpPack_AccountLogin accountID:%s",accountID);
        // this.LocalDataManager.SetConfigProperty("Account", "AccessTokenInfo", accessTokenInfo);
        // this.LocalDataManager.SetConfigProperty("Account", "AccountID", accountID);

        //如果是公司内部账号,需要保存账号记录
        if (accountType == SDK.SDKType_Company) {
            this.AddNewAccountList(this.dataInfo["CharAccount"], this.dataInfo["Token"])
        }

        console.log("========开始链接大厅服务(HeroAccountManager)==========" + JSON.stringify(serverPack));
        NetManager.Instance.InitConnectServer(); 
    }

     /**
     * 增加本地缓存账号
     * @param account
     * @param psw
     */
     AddNewAccountList (account:string, token:string) {
        // if (this.logInAccountList.indexOf(account) !== -1){
        //     this.logInAccountList.Remove(account);
        //     this.logInAccountList.push(account);
        // }
        // else {
        //     this.logInAccountList.push(account);
        // }

        // this.localAccountDict[account] = token;

        // this.SavePlayerAccount();
    }

    // /**
    //  * 删除本地账号
    //  * @param account
    //  */
    // DeleteLocalAccount (account) {

    //     if (this.logInAccountList.InArray(account)) {
    //         this.logInAccountList.Remove(account);

    //     }
    //     delete this.localAccountDict[account];

    //     this.SavePlayerAccount();

    // }

    // /**
    //  * 清除本地所有账号
    //  * @param account
    //  */
    // DeleteAllLocalAccount () {
    //     this.localAccountDict = {};
    //     this.logInAccountList = [];
    //     this.SavePlayerAccount();
    // }

    //连接服务器失败
    Event_ConnectFail (event) {
        console.log("大厅连接服务器失败");
        this.IsDoLogining(false);
        this.UpdateAccessPoint();
    }

    Event_ConnectHttpFail (event) {
        this.IsDoLogining(false);
        this.UpdateAccessPoint();
    }
    UpdateAccessPoint () {
        let now = new Date().getTime();
        // if (now - this.lastChangeTime < 5000) {
        //     return;
        // }
        // this.lastChangeTime = now;
        // this.ChangeAccessPoint();
        // app.NetManager().InitServerAddress();
    }

    //设置是否是在登录
    IsDoLogining (state) {
        // this.SysLog("IsDoLogining state:%s", state);
        // this.isDoLogin = state;
        // if (state) {
        //     app.Client.OnEvent("ChangeBtnState", { "state": 1 });
        // } else {
        //     app.Client.OnEvent("ChangeBtnState", { "state": 0 });
        // }
    }
}
